package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.dao.OrderNameDao;
import com.pgytesting.secondhandcar.dao.ServiceInformationDao;
import com.pgytesting.secondhandcar.model.OrderName;
import com.pgytesting.secondhandcar.model.ServiceInformation;
import com.pgytesting.secondhandcar.service.OrderNameService;
import com.pgytesting.secondhandcar.service.ServiceInformationService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务信息(ServiceInformation)表服务实现类
 *
 * @author makejava
 * @since 2019-08-12 18:55:58
 */
@Slf4j
@Service("serviceInformationService")
public class ServiceInformationServiceImpl implements ServiceInformationService {
    @Resource
    private ServiceInformationDao serviceInformationDao;

    @Resource
    private OrderNameDao orderNameDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ServiceInformation queryById(String id) {
        return this.serviceInformationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ServiceInformation> queryAllByLimit(int offset, int limit) {
        return this.serviceInformationDao.queryAllByLimit(offset, limit);
    }

    /**
     * 条件查询多条数据
     *
     * @param serviceInformation 实例对象
     * @return 对象列表
     */
    @Override
    public List<ServiceInformation> queryAll(ServiceInformation serviceInformation) {
        return this.serviceInformationDao.queryAll(serviceInformation);
    }

    /**
     * 新增数据
     *
     * @param serviceInformation 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public ResponseMessage insert(ServiceInformation serviceInformation) {
        // 获取登陆用户账号
        String phoneNumber = SessionUtil.getSessionMcpCar().getPhoneNumber();
        // 获取订单id
        String orderId = serviceInformation.getId();
        // 生成服务id
        serviceInformation.setId(UtilMethod.getUniqueCode());
        serviceInformation.setCreatedBy(phoneNumber);
        OrderName updateOrder = new OrderName();
        //订单id
        updateOrder.setId(orderId);
        // 服务id
        updateOrder.setServiceInformationId(serviceInformation.getId());
        // 修改订单状态
        updateOrder.setOrderStatus(Parameters.ORDER_22002);
        // 用户ID
        updateOrder.setUpdatedBy(phoneNumber);
        // 插入车辆补充信息
        this.serviceInformationDao.insert(serviceInformation);
        orderNameDao.update(updateOrder);
        return ResponseMessage.successResponse("补充车辆服务信息成功");
    }

    /**
     * 修改数据
     *
     * @param serviceInformation 实例对象
     * @return 实例对象
     */
    @Override
    public ServiceInformation update(ServiceInformation serviceInformation) {
        this.serviceInformationDao.update(serviceInformation);
        return this.queryById(serviceInformation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.serviceInformationDao.deleteById(id) > 0;
    }
}